/**
 * All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
 *
 *MpmProjectSquaredController
 * 销售项目九宫格
 *
 * 修改纪录
 * 2022-11-09 版本：1.0 houyuhang 创建。
 * @version 版本：1.0
 * @author 作者：houyuhang
 * 创建日期：2022-11-09
 */

package com.topscomm.mpm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.topscomm.cbo.pojo.CboItemDetailsEntity;
import com.topscomm.cbo.pojo.CboItemsEntity;
import com.topscomm.cbo.service.ICboItemDetailsService;
import com.topscomm.cbo.service.ICboItemsService;
import com.topscomm.mpm.pojo.MpmProjectSquaredEntity;
import com.topscomm.pub.util.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.topscomm.mpm.controller.auto.MpmProjectSquaredControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: houyuhang
 * @date: 2022-11-09
 * @description:销售项目九宫格Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectSquared")
public class MpmProjectSquaredController extends MpmProjectSquaredControllerAuto implements IMpmProjectSquaredController {
	@Autowired
	private ICboItemsService iCboItemsService;
	@Autowired
	private ICboItemDetailsService iCboItemDetailsService;
	/**
	 * @author: houyuhang
	 * @date: 2022-11-09
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: houyuhang
	 * @date: 2022-11-09
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @author: houyuhang
	 * @date: 2022-11-09
	 * @param paraMap
	 * @return
	 * @description:获取九宫格最新信息
	 * @modify:
	 */
	@GetMapping(value = "/queryLatestInfo")
	public ResponseResult<Object> queryLatestInfo(@RequestParam(required = false) Map<String, Object> paraMap) {
		String taskid = ConvertUtil.convertToString(paraMap.get("qry_taskid"));
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" taskid = '{0}' and version = (select max(version) from mpmprojectsquared where taskid = '{0}') ", taskid);
		List<MpmProjectSquaredEntity> mpmProjectSquaredEntities = this.service.queryByWhere(sql.toString());
		return ResponseResult.ok(mpmProjectSquaredEntities);
	}

	/**
	 * @author: houyuhang
	 * @date: 2022-11-09
	 * @param paraMap
	 * @return
	 * @description:保存九宫格信息
	 * @modify:
	 */
	@PostMapping(value = "/doSaveSquared")
	public ResponseResult<Object> doSaveSquared(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			this.service.doSaveSquared(paraMap);
			return ResponseResult.ok("保存成功!");
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
	/**
	 * @description: 重写datacodelist
	 * @param paraMap
	 * @return
	 * @author: houyuhang
	 * @date: 2022年11月09日
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/dataCodeList")
	public ResponseResult<Object> dataCodeList(@RequestParam(required = false) Map<String, Object> paraMap) {
		ResponseResult<Object> res = super.dataCodeList(paraMap);
		Map<String, Object> codeMap = (Map<String, Object>) res.getResult();
		// 部门名称编码，由于redis未存储sortcode，为了防止deptcommoncode顺序混乱,从数据库中查询deptcommoncode
		List<Map<String, Object>> deptcommoncode1List = new ArrayList<Map<String, Object>>();
		CboItemsEntity deptItemsEntity = iCboItemsService.queryObjectByWhere("code = 'DeptCommonCodeTrack'");
		List<CboItemDetailsEntity> deptCommonCodes = iCboItemDetailsService.queryByWhere("itemid=" + deptItemsEntity.getId() + " and enabled=1 order by sortcode asc");
		for (CboItemDetailsEntity codeItem : deptCommonCodes) {
			Map<String, Object> addItem = new HashMap<>();
			addItem.put("id", codeItem.getId());
			addItem.put("code", codeItem.getCode());
			addItem.put("name", codeItem.getName());
			deptcommoncode1List.add(addItem);
		}
		codeMap.put("deptCommonCodeList", deptcommoncode1List);
		return ResponseResult.ok(codeMap);
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-11-14
	 * @param paraMap
	 * @return
	 * @description:九宫格数据获取
	 * @modify:
	 */
	@GetMapping(value = "/querySquaredInfo")
	public ResponseResult<Object> querySquaredInfo(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> res = this.service.querySquaredInfo(paraMap);
			return ResponseResult.ok(res);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
}

